
   var recorder = null;
    var recorderTimer = null;
    var time = 0;
	
	
function myluyin(url,anzhu,luwan){
	this.anzhu = '';
	if(anzhu){
		this.anzhu = anzhu;
	}
	this.luwan = '';
	if(luwan){
		this.luwan = luwan;
	}
	if(url){
		this.url = url;
	}
	var that = this;
	this.time = 0;
	this.recorderTimer = setInterval(function(){
		that.time++;
		if(that.anzhu){
			that.anzhu(that.time);
		}
	},1000);
	this.recorder = plus.audio.getRecorder();
	this.recorder.record({filename:"_doc/audio/"},function(filePath){
	    plus.io.resolveLocalFileSystemURL(filePath,function(entry){
			var task = plus.uploader.createUpload(that.url, {method: "POST"}, function (t, status) {
					var res = t.responseText;
					// res = JSON.stringify(res);
					res = JSON.parse(res);
					res[0].yuyin_time = that.time;
					// console.log(res[0].path);
				if(that.luwan){
					that.luwan(res[0])
				}
				// 上传完成
			    // if (status == 200) {
			    //     alert("Upload success: " + t.url);
			    // } else {
			    //     alert("Upload failed: " + status);
			    // }
			});
			task.addFile(filePath, {key: "file"}); // 以表单的方法上传整个音频
			task.start();
			
			
		 		//  createUpload(filePath,that.url,function(data){
					//  if(that.luwan){
					// 	 that.luwan(data)
					//  }
					//  
				 // })  // 上传语音
	    },function(e){
	        console.log("读取录音失败："+e.message);
	    });
	});
	
	this.endRecord =function(){
		clearInterval(that.recorderTimer);
		that.recorder.stop();
	}
	
	
	$("#btnStartRecord").bind('touchend',function(){
	    that.endRecord();
	});
	
}	
	
	
		// 上传语言   发送语音
	function createUpload(file,url,func) {
	    var task = plus.uploader.createUpload(url, {method: "POST"}, function (t, status) {
			// mui.alert()
			if(func){
				func(t)
			}
			// 上传完成
	        // if (status == 200) {
	        //     alert("Upload success: " + t.url);
	        // } else {
	        //     alert("Upload failed: " + status);
	        // }
	    });
	    task.addFile(file, {key: "aaa"}); // 以表单的方法上传整个音频
	    task.start();
	}		





//拍照 发送图片
     function getImages(url,func) {
     	var mobileCamera = plus.camera.getCamera();
     	mobileCamera.captureImage(function(e) {
     		plus.io.resolveLocalFileSystemURL(e, function(entry) {
     			var path = entry.toLocalURL() + '?version=' + new Date().getTime();
     			uploadimgss(url, path, function(data) {
     				if (func) {
     					func(data)
     				}
     			}) // 群聊天发送图片
     		}, function(err) {
     			console.log("读取拍照文件错误");
     		});
     	}, function(e) {
     		console.log("er", err);
     	}, function() {
     		filename: '_doc/head.png';
     	});
     }



     // 这个是选择图片上传
     function galleryImages(url, func) {
     	console.log("你选择了从相册选择");
     	plus.gallery.pick(function(a) {
     		plus.io.resolveLocalFileSystemURL(a, function(entry) {
     			plus.io.resolveLocalFileSystemURL('_doc/', function(root) {
     				root.getFile('head.png', {}, function(file) {
     					//文件已经存在
     					file.remove(function() {
     						console.log("文件移除成功");
     						entry.copyTo(root, 'head.png', function(e) {
     							var path = e.fullPath + '?version=' + new Date().getTime();
     							uploadimgss(url, path, function(data) {
     								if (func) {
     									func(data)
     								}
     							}) // 群聊天发送图片

     						}, function(err) {
     							console.log("copy image fail: ", err);
     						});
     					}, function(err) {
     						console.log("删除图片失败：（" + JSON.stringify(err) + ")");
     					});
     				}, function(err) {
     					//打开文件失败
     					entry.copyTo(root, 'head.png', function(e) {
     						var path = e.fullPath + '?version=' + new Date().getTime();
     						uploadHeadImg(path);
     					}, function(err) {
     						console.log("上传图片失败：（" + JSON.stringify(err) + ")");
     					});
     				});
     			}, function(e) {
     				console.log("读取文件夹失败：（" + JSON.stringify(err) + ")");
     			});
     		});
     	}, function(err) {
     		console.log("读取拍照文件失败: ", err);
     	}, {
     		filter: 'image'
     	});
     };


     //压缩图片转成base64
     function getBase64Image(img) {
     	// console.log(img.getBase64Image());
     	var canvas = document.createElement("canvas");
     	var width = img.width;
     	var height = img.height;
     	var size = img.size;
     	if (width > height) {
     		if (width > 500) {
     			height = Math.round(height *= 500 / width);
     			width = 500;
     		}
     	} else {
     		if (height > 500) {
     			width = Math.round(width *= 500 / height);
     		}
     		height = 500;
     	}
     	canvas.width = width;
     	canvas.height = height;
     	var ctx = canvas.getContext('2d');
     	ctx.drawImage(img, 0, 0, width, height);
     	var dataUrl = canvas.toDataURL('image/png', 0.8);
     	return dataUrl.replace('data:image/png:base64,', '');
     }



     //聊天发送图片或者拍照上传到后台    第一个是后台地址   第二个是图片地址  第三个是回调
     function uploadimgss(url, path, func) {
     	var images = new Image();
     	images.src = path;
     	images.onload = function() {
     		var imgData = getBase64Image(images);
     		console.log(imgData)
     		plus.nativeUI.showWaiting();
     		mui.ajax(url, {
     			data: {
     				file: imgData
     			},
     			dataType: 'json', //服务器返回json格式数据
     			type: 'post', //HTTP请求类型
     			timeout: 10000, //超时时间设置为10秒；
     			success: function(data) {
     				console.log("上传数据")
     				plus.nativeUI.closeWaiting()
     				if (func) {
     					func(data)
     				}

     			},
     			error: function(data) {
     				mui.alert(data + "1")
     			}
     		});
     	}

     }
